#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################


rm(list=ls())
rm(.Random.seed)

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
R_RESULTS_PATH <- file.path(R_ROOT, "Resultats")

R_STATIC_DIR <- file.path(R_DATA_PATH, "Bylot/Fixe")
R_MOBILE_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")


#'#
#'# Chargement des dependances
#'# 

source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))

DATA_PATH <- file.path(R_RESULTS_PATH, "HRCompare/v6")
DEST_PATH <- file.path(R_RESULTS_PATH, "HRCompare/final")

statsCols <- c("sizeRatio", "overlapIndex", "propInRef", "comparedArea", "refArea")


#'#################################################################################################
#'#                                                                                              ##
#'#                                 Compilation                                                  ##
#'#                                                                                              ##
#'#################################################################################################

#'#
#'# Simulations
#'# Compilation a partir des fichiers bruts et sauvegarde
#'# (Compilation des fichiers bruts via la fonction (loadSimulationFiles dans loadData.R) 
#'# 


mob1 <- fread(file.path(DATA_PATH, "HR_compare_tracksGenMobileLC32_compiled.csv"))
mob2 <- fread(file.path(DATA_PATH, "HRCompare", "HR_compare_tracksGenMobileLC32_compiled2.csv"))
hrStatsTracksGenMobileLC32 <- rbindlist(list(mob1, mob2))
hrStatsTracksGenMobileLC32[, type := as.factor(type)]
hrStatsTracksGenMobileLC32[, method := as.factor(method)]
hrStatsTracksGenMobileLC32[, deployment := as.factor(deployment)]
hrStatsTracksGenMobileLC32[, iteration := as.factor(iteration)]
setkey(hrStatsTracksGenMobileLC32, method, percent, type, deployment)
save(hrStatsTracksGenMobileLC32, file=file.path(DATA_PATH, "HR_compare_tracksGenMobileLC32_all.RData"))


stat1 <- fread(file.path(DATA_PATH, "HR_compare_tracksGenStaticLC32_compiled.csv"))
stat2 <- fread(file.path(DATA_PATH, "HR_compare_tracksGenStaticLC32_compiled2.csv"))
hrStatsTracksGenStaticLC32 <- rbindlist(list(stat1, stat2))
hrStatsTracksGenStaticLC32 [, type := as.factor(type)]
hrStatsTracksGenStaticLC32 [, method := as.factor(method)]
hrStatsTracksGenStaticLC32 [, deployment := as.factor(deployment)]
hrStatsTracksGenStaticLC32 [, iteration := as.factor(iteration)]
setkey(hrStatsTracksGenStaticLC32, method, percent, type, deployment)
save(hrStatsTracksGenStaticLC32 , file=file.path(DATA_PATH, "HR_compare_tracksGenStaticLC32_all.RData"))



#'#################################################################################################
#'#                                                                                              ##
#'#                                 Chargement des donnees                                       ##
#'#                                                                                              ##
#'#################################################################################################


#' hrStatsTracksGenMobileLC32
load(file.path(DATA_PATH, "HR_compare_tracksGenMobileLC32_all.RData"))

#' hrStatsTracksGenStaticLC32
load(file.path(DATA_PATH, "HR_compare_tracksGenStaticLC32_all.RData"))



#'#
#'# Donnees pour statistiques (moyenne par deploiement)
#'# 

hrStatsTracksMobile <- compileHRStats(hrStatsTracksGenMobileLC32, statsCols, statBy=quote(list(type, method, percent, deployment)))
setkey(hrStatsTracksMobile, method, percent, deployment)
hrStatsTracksMobile[, type := factor(type, levels=c("Raw", "LC32", "LC321",
            "DAF","SF", "TrStat", "TrMob"))]
save(hrStatsTracksMobile, file=file.path(DEST_PATH, "hrStatsTracksMobile.RData"))
#write.csv(statsTracksMobile, file.path(DATA_PATH, "statsTracksMobile.csv"), row.names=FALSE)


hrStatsTracksStatic <- compileHRStats(hrStatsTracksGenStaticLC32, statsCols, statBy=quote(list(type, method, percent, deployment)))
setkey(hrStatsTracksStatic, method, percent, deployment)
hrStatsTracksStatic[, type := factor(type, levels=c("Raw", "LC32", "LC321",
            "DAF","SF", "TrStat", "TrMob"))]
save(hrStatsTracksStatic, file=file.path(DEST_PATH, "hrStatsTracksStatic.RData"))
#write.csv(statsTracksStatic, file.path(DATA_PATH, "statsTracksStatic.csv"), row.names=FALSE)


#'#
#'# Donnees pour graphiques (moyennes globales sur toutes les donnees)
#'# 

#' hrGraphsTracksFilter
load(file.path(DEST_PATH, "hrGraphsTracksFilter.RData"))

tmp <- NULL
for (i in statsCols) {
    tmp <- rbind(tmp, cbind(hrStatsTracksGenMobileLC32[, meanICl3(.SD, i, byG2="deployment", byG3="iteration"), by=list(type, method, percent)], stat=i))
    tmp <- rbind(tmp, cbind(hrStatsTracksGenStaticLC32[, meanICl3(.SD, i, byG2="deployment", byG3="iteration"), by=list(type, method, percent)], stat=i))
}
tmp[, type := factor(type, levels=c("Ref", "Raw", "LC32", "LC321",
            "DAF","SF", "TrStat", "TrMob"))]
tmp[, stat := as.factor(stat)]

hrGraphsTracksAll <- rbindlist(list(hrGraphsTracksFilter, tmp))


#write.csv(res2, file.path(R_RESULTS_PATH, "HRCompare/hrGraphsTracksAll.csv"), row.names=FALSE)
save(hrGraphsTracksAll, file=file.path(DEST_PATH, "hrGraphsTracksAll.RData"))


#'#
#'# Simulations cercles
#'# 

radius <- c("250", "500", "750", "1000", "2500", "5000")

res <- lapply(radius, compileRadiusData, statsCols=statsCols, path=DATA_PATH)

hrStatsRadius <- rbindlist(lapply(res, function(x){x$stat}))
hrStatsRadius[, type := factor(type, levels=c("rad250", "rad500", "rad750",
            "rad1000","rad2500", "rad5000"))]
save(hrStatsRadius, file=file.path(DEST_PATH, "hrStatsRadius.RData"))

hrGraphsRadius <- rbindlist(lapply(res, function(x){x$graph}))
hrGraphsRadius[, type := factor(type, levels=c("rad250", "rad500", "rad750",
            "rad1000","rad2500", "rad5000"))]
save(hrGraphsRadius, file=file.path(DEST_PATH, "hrGraphsRadius.RData"))


